//
//  MarriagePlanTableHeaderView.m
//  BaseProject
//
//  Created by mac on 2020/11/20.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "MarriagePlanTableHeaderView.h"
#import "MarriagePlanModel.h"
#import <BRPickerView.h>
@interface MarriagePlanTableHeaderView ()
@property(nonatomic,strong)UIImageView *bgIV;
@property(nonatomic,strong)UILabel *overDateLab;
@property(nonatomic,strong)UILabel *dateLab;
@property(nonatomic,strong)BaseAdjustButton *editeButton;
@property(nonatomic,strong)UILabel *taskLab;
@property(nonatomic,strong)UILabel *taskNumLab;
@property(nonatomic,strong)UIView *taskBGView;
@property(nonatomic,strong)UIView *taskProgressView;
@property(nonatomic,strong)BaseAdjustButton *taskButton;
@end

@implementation MarriagePlanTableHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WhiteColor;
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    
}
- (void)createUI
{
    [self bgIV];
    [self taskButton];
    
    UIView *view = [[UIView alloc]init];
    [self addSubview:view];
    view.backgroundColor = WhiteColor;
    view.frame = CGRectMake(0, self.height - 10*ADAPTER_WIDTH, self.width, 50*ADAPTER_WIDTH);
    view.layer.cornerRadius = 10*ADAPTER_WIDTH;
    //view.layer.masksToBounds = YES;
    //view.layer.shadowColor = rgba(230, 230, 230, 1).CGColor;
    //view.layer.shadowOffset = CGSizeMake(0, -2);
    //view.layer.shadowRadius = 5;
    //view.layer.shadowOpacity = 1;
    self.clipsToBounds = YES;
}
- (void)setPlanModel:(MarriagePlanModel *)planModel
{
    _planModel = planModel;
    
    if (planModel.data.marryDate.length == 0) {
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"还未设置你的婚期"]];
        titleAttributes.font = kFontRegular(22*ADAPTER_WIDTH);
        titleAttributes.color = WhiteColor;
        self.overDateLab.attributedText = titleAttributes;
        self.dateLab.text = [NSString stringWithFormat:@"婚期：%@",@"未设置"];
    } else {
        [self updateWithNewTime:planModel.data.marryDate];
        self.dateLab.text = [NSString stringWithFormat:@"婚期：%@",planModel.data.marryDate];
    }
    [self editeButton];
    self.taskLab.text = @"婚礼任务";
    [self taskBGView];
    
    //任务
    {
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%ld",planModel.data.marryOverTaskTotal]];
        titleAttributes.font = kFontRegular12;
        titleAttributes.color = WhiteColor;
        
        NSMutableAttributedString *titleAttributes1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"/%ld",planModel.data.marryTaskTotal]];
        titleAttributes1.font = kFontRegular12;
        titleAttributes1.color = rgba(255, 255, 255, 0.8);
        
        [titleAttributes appendAttributedString:titleAttributes1];
        
        self.taskNumLab.attributedText = titleAttributes;
        
        
        CGFloat percent = (CGFloat)planModel.data.marryOverTaskTotal / (CGFloat)planModel.data.marryTaskTotal;
        if (percent > 0) {
            [self taskBGView];
            [self taskProgressView];
            [UIView animateWithDuration:.8 animations:^{
                self.taskProgressView.width = percent * self.taskBGView.width;
            }];
        }
    }
}
- (void)updateWithNewTime:(NSString *)time
{
    self.dateLab.text = [NSString stringWithFormat:@"婚期：%@",time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentString = [formatter stringFromDate:[NSDate date]];
    
    NSDate *selectedDate = [NSDate dateWithString:time format:@"yyyy-MM-dd"];
    NSDate *currentDate = [NSDate dateWithString:currentString format:@"yyyy-MM-dd"];
    NSTimeInterval tmp = ceil([selectedDate timeIntervalSince1970]) - ceil([currentDate timeIntervalSince1970]);
    NSInteger day = (NSInteger)(tmp/(NSTimeInterval)(60*60*24));
    
    self.planModel.data.marryOverDate = day;
    if (day <= 0) {
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"已完婚"]];
        titleAttributes.font = kFontRegular(22*ADAPTER_WIDTH);
        titleAttributes.color = WhiteColor;
        self.overDateLab.attributedText = titleAttributes;
    } else {
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"距离婚礼还有天"]];
        titleAttributes.font = kFontRegular(22*ADAPTER_WIDTH);
        titleAttributes.color = WhiteColor;
        
        NSMutableAttributedString *titleAttributes1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%ld",self.planModel.data.marryOverDate]];
        titleAttributes1.font = kFontRegular(32*ADAPTER_WIDTH);
        titleAttributes1.color = WhiteColor;
        
        [titleAttributes insertAttributedString:titleAttributes1 atIndex:6];
        self.overDateLab.attributedText = titleAttributes;
    }
}
#pragma mark - click
- (void)clickEditeButton:sender
{
    !self.clickEditeButtonBlock ?: self.clickEditeButtonBlock ();
}
- (void)clickTaskButton:sender
{
    !self.clickTaskButtonBlock ?: self.clickTaskButtonBlock ();
}
#pragma mark - lazy懒加载
- (UIImageView *)bgIV
{
    if (!_bgIV) {
        UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self addSubview:view];
        view.image = [UIImage getPNGimageInBundleWithName:@"MarrayPlan_header"];
        _bgIV = view;
    }
    return _bgIV;
}
- (UILabel *)overDateLab
{
    if (!_overDateLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(18*ADAPTER_WIDTH, 77*ADAPTER_WIDTH + kStatusBarHeight, self.width - 30*ADAPTER_WIDTH, 45*ADAPTER_WIDTH);
        _overDateLab = lab;
    }
    return _overDateLab;
}
- (UILabel *)dateLab
{
    if (!_dateLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = WhiteColor;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        //lab.frame = CGRectMake(18*ADAPTER_WIDTH, 113*ADAPTER_WIDTH + kStatusBarHeight, self.width - 30*ADAPTER_WIDTH, 17*ADAPTER_WIDTH);
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(18*ADAPTER_WIDTH);
            make.top.mas_equalTo(123*ADAPTER_WIDTH + kStatusBarHeight);
        }];
        _dateLab = lab;
    }
    return _dateLab;
}
- (BaseAdjustButton *)editeButton
{
    if (!_editeButton) {
        BaseAdjustButton *button = [BaseAdjustButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        button.type = layoutTypeOnlyImage;
        button.imageWidth = 15*ADAPTER_WIDTH;
        button.imageHeight = 15*ADAPTER_WIDTH;
        button.marginLeft = 2*ADAPTER_WIDTH;
        button.marginRight = 30*ADAPTER_WIDTH;
        button.marginTop = 10*ADAPTER_WIDTH;
        button.marginBottom = 10*ADAPTER_WIDTH;
        [button begin];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.dateLab.mas_right).mas_equalTo(0);
            make.centerY.mas_equalTo(self.dateLab).mas_equalTo(-2*ADAPTER_WIDTH);
        }];
        [button setImage:[UIImage getPNGimageInBundleWithName:@"MarrayPlan_Edite"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickEditeButton:) forControlEvents:UIControlEventTouchUpInside];
        _editeButton = button;
    }
    return _editeButton;
}
- (UILabel *)taskLab
{
    if (!_taskLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = WhiteColor;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(18*ADAPTER_WIDTH, 153*ADAPTER_WIDTH + kStatusBarHeight, 60*ADAPTER_WIDTH, 13*ADAPTER_WIDTH);
        _taskLab = lab;
    }
    return _taskLab;
}
- (UIView *)taskBGView
{
    if (!_taskBGView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = rgba(255, 255, 255, 1);
        view.frame = CGRectMake(76*ADAPTER_WIDTH, 0, 170*ADAPTER_WIDTH, 10*ADAPTER_WIDTH);
        view.centerY = self.taskLab.centerY;
        [self addSubview:view];
        view.layer.cornerRadius = 5*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        _taskBGView = view;
    }
    return _taskBGView;
}
- (UILabel *)taskNumLab
{
    if (!_taskNumLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(self.taskBGView.right + 6*ADAPTER_WIDTH, 0, 100*ADAPTER_WIDTH, 13*ADAPTER_WIDTH);
        lab.centerY = self.taskLab.centerY;
        _taskNumLab = lab;
    }
    return _taskNumLab;
}
- (UIView *)taskProgressView
{
    if (!_taskProgressView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = rgba(255, 134, 134, 1);
        view.frame = CGRectMake(0, 0, 0, 10*ADAPTER_WIDTH);
        [self.taskBGView addSubview:view];
        view.layer.cornerRadius = 5*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        _taskProgressView = view;
    }
    return _taskProgressView;
}
- (BaseAdjustButton *)taskButton
{
    if (!_taskButton) {
        BaseAdjustButton *button = [BaseAdjustButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        button.title = @"任务";
        button.fontWeight = UIFontWeightRegular;
        button.fontSize = 14*ADAPTER_WIDTH;
        button.imageWidth = 12*ADAPTER_WIDTH;
        button.imageHeight = 12*ADAPTER_WIDTH;
        button.type = layoutTypeHorizontalLeftImageRightTitle;
        button.marginLeft = 16*ADAPTER_WIDTH;
        button.marginItemX = 4*ADAPTER_WIDTH;
        button.marginRight = 20*ADAPTER_WIDTH;
        button.marginTop = 6*ADAPTER_WIDTH;
        button.marginBottom = 6*ADAPTER_WIDTH;
        [button begin];
        [button setTitleColor:rgba(253, 122, 189, 1) forState:UIControlStateNormal];
        [button setImage:[UIImage getPNGimageInBundleWithName:@"MarrayPlan_add"] forState:UIControlStateNormal];
        button.backgroundColor = WhiteColor;
        button.layer.cornerRadius = button.height * 0.5;
        button.layer.masksToBounds = YES;
        button.bottom = self.height - 68*ADAPTER_WIDTH;
        button.right = WIDTH + 13*ADAPTER_WIDTH;
        [button addTarget:self action:@selector(clickTaskButton:) forControlEvents:UIControlEventTouchUpInside];
        _taskButton = button;
    }
    return _taskButton;
}
@end
